checkArrayVal
Arrays Sandbox PanosP Revised

Checking if an Element Is in an Array

Problem
=======

We want to check if a given value exists
as some element's value in a given array.

Solution
========

It's not as simple as checking for key
existence. We must iterate the array elements
to tell if a given string exists as a given array
element's value:

  function arrayValueExists(arr, val,		i) {
  	for (i in arr) {
  		if (val == arr[i])
  			return(1)
  	}
  	return(0)
  }

Discussion
==========

Function returns zero if the given value is not
an element of the given array. In that case *all*
of the array elements must be visited to tell so.
In the contrary, if the value exists, then the
function returns a non zero value (1) just
when the corresponding element is visited.
It would be of much help to return the
index of the value found, but in that case
we must take care of what to return if the value
doesn't exist. However, it's a simple thing
to implement such an approach, e.g.
given an array where there is not an empty string
indexed element, the function would look like:

 function arrayValueExists(arr, val,		i) {
  	for (i in arr) {
  		if (val == arr[i])
  			return(i)
  	}
  	return("")
 }

Keep in mind, that there may exist more than one
elements with the same value. As a last step in
this topic, we'll introduce a function to return
the _number of occurences_ of the given value,
instead of just telling if the value exists:

 function arrayValueOccurences(arr, val,	i, n) {
 	for (i in arr) {
 		if (val == arr[i])
 			n++
 	}
 	return(n + 0)
 }

The above function returns the number of elements
of the specified value, or zero when no such elements
exist in the array. Note, however, that all of the
elements must be visited.

Author
======

Panos Papadopoulos

